<template>
    <el-dialog v-model="dialogFormVisible" :title="form.id ? '编辑用户' : '添加用户'" width="500">
        <el-form :model="form">
            <itemInput label="用户名称" v-model="form.user_name" :disabled="form.id > 0" />
            <itemInput label="密码" v-model="form.password" password />
            <itemInput label="昵称" v-model="form.nickname" />
            <itemInput label="手机号" v-model="form.phone" />
            <itemInput label="邮箱" v-model="form.email" />
            <itemSwitch label="状态" v-model="form.status" />
            <item label="角色">
                <el-select v-model="form.role_ids" multiple placeholder="选择角色" style="width: 240px">
                    <el-option v-for="item in roleOptions" :key="item.id" :label="item.role_name" :value="item.id" />
                </el-select>
            </item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button type="primary" @click="submits">提交</el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script>
import itemInput from '@/components/form/item.input.vue'
import itemSwitch from '@/components/form/item.switch.vue'
import item from '@/components/form/item.vue'
export default {
    emits: ["refresh"],
    data() {
        return {
            formLabelWidth: '120px',
            dialogFormVisible: false,
            roleOptions: [],// 角色列表
            form: {}
        }
    },
    methods: {
        // 打开弹窗显示
        Open(res = {}) {
            this.form = JSON.parse(JSON.stringify(res));
            this.dialogFormVisible = true;
            var that = this
            if(this.roleOptions.length == 0){
                that.$http.get("/user/role_list").then(res=>{
                    that.roleOptions = res.data
                })
            }
        },
        // 提交表单
        submits() {
            var that = this
            that.$http.post("/user/create",that.form).then(res=>{
                if(that.form.id){
                    that.$func.success("修改成功")
                } else {
                    that.$func.success("创建成功")
                }
                that.dialogFormVisible = false
                that.$emit("refresh",null)
            })
        }
    },
    components: {
        itemInput,
        itemSwitch,
        item,
    },
}
</script>